<?php

namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class DimissionController extends BaseController {
    public function index(){
		$Mdo = D("Admin/User");
		$ulist = null;
		$wheres = array();
		$user = I('get.user', '', 'trim');
		$number = I('get.number', '', 'trim');
		$nickname= I('get.nickname', '', 'trim');
		$leavetype = I('get.leaveType', -2, 'intval');
		$role = I('get.role', -1,'intval');
		$sector = I('get.sector', -1, 'intval');
		$dimissiontime1=I('get.dimissiontime1','','trim');
		$dimissiontime2=I('get.dimissiontime2','','trim');
		$psize = I('get.psize', $this->$m_psize,'intval');
		if (!$psize)
			$psize = $this->m_psize;
		else if ( $psize > 20 )
			$psize = $this->m_psize;
		if($number){
			$wheres['user_extension.ext_yggh'] = array('like' , '%'.$number.'%');
		}
		if($nickname){
			$wheres['user_extension.ext_name'] = array('like', '%'.$nickname.'%');
		}
		if(-2 !=$leavetype){
			$wheres['dimission.dim_type'] = array('eq' , $leavetype);
		}
		if(-1 != $sector) {
			if($sector != 1){
				$sidArr = array($sector);
				$ziId = M('sector')->where('parent_sector='.$sector)->field('sector_id')->select();
				// dump($ziId);
				if($ziId){
					foreach ($ziId as  $value) {
						foreach ($value as $key => $val) {
							array_push($sidArr,$value[$key]);
						}
					}	
				}
				// $wheres['user.sector_id'] = array('eq' , $sector);
				$wheres['user.sector_id'] = array('in' , $sidArr);
			}
		}
		/*if (-1 != $sector )
			$wheres['user.sector_id'] = array('eq' , $sector);*/
		if ( $role != -1 )
		    $wheres['user.role_id'] = array('eq' , $role);
		if($dimissiontime1 && $dimissiontime2)
			$wheres['dimission.dim_date']=array('between', array($dimissiontime1,$dimissiontime2));
		if($dimissiontime1 && $dimissiontime2=='')
            $wheres['dimission.dim_date']=array('EGT',$dimissiontime1);
		if($dimissiontime1=='' && $dimissiontime2){
		    $wheres['dimission.dim_date']=array('ELT',$dimissiontime2);
		}
		$wheres['user.state'] = array('eq', -1 );
		$wheres['dim_status'] =array('eq',1);
		$cnt = $Mdo
		        ->join('LEFT JOIN user_extension on user.user_id= user_extension.user_id')
		        ->join('LEFT JOIN dimission on user.user_id= dimission.user_id')
		        ->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$ulist = $Mdo->field("user.user_id,user.login_name,user.nickname,user.sector_id,user.role_id,user.phone,user.qq,user.weixin,user.email,user.state,user.last_time,role.role_name as rname,sector.sector_name as sname,user_extension.ext_yggh,user_extension.ext_name,dimission.dim_type,dimission.dim_reason,dimission.dim_beizhu,dimission.dim_date,dimission.dim_addtime,dimission.dim_id,dimission.dim_status")
		               ->where( $wheres )
					   ->order('dimission.dim_addtime desc')
					   ->join('LEFT JOIN role on role.role_id = user.role_id')
					   ->join('LEFT JOIN sector on sector.sector_id=user.sector_id')
					   ->join('LEFT JOIN user_extension on user_extension.user_id = user.user_id')
					    ->join('LEFT JOIN dimission on dimission.user_id = user.user_id')
					   ->limit($Page->firstRow.','.$Page->listRows )
					   ->select();
				/*$u = $Mdo->	getlastsql($ulist)  ; */
		if( $ulist ){
			$res['rcode'] = 1;
			$res['data'] = $ulist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "用户记录为空";
		}
		e_json($res);
  }
	//获取页面操作参数
	public function getPageParam(){
		$res = array('rcode'=>1,'sector'=>array(), 'role'=>array(),'msg'=>'');
		$wheres = array();
		$wheres['is_deleted'] = array('neq', 1);
		$MRole = D('Admin/Role');
		$roles = $MRole->field("role_id,role_name,parent_role")->where( $wheres )->select();
		$_roles = array();
        foreach( $roles as $vs ){
            $_roles[$vs['role_id']] = $vs;
        }
       if( 1 == $this->m_user['user_id'])
	       $arr = list_to_tree($roles,'role_id','parent_role', '_child', 0);
       else{
       		$arr = array();
       		$arr[] = $_roles[$this->m_user['role_id']];
		    $arr[0]['_child'] =  list_to_tree($roles,'role_id','parent_role', '_child', $this->m_user['role_id']);
       }
	   $res['role'] = $arr;
	   $MSector = D('Admin/Sector');
	   $secs = $MSector->field("sector_id,info,sector_name,parent_company,parent_sector,manager_id")->where( $wheres )->select();
       $_secs = array();
	   $arr = array();
    	foreach ($secs as $sc ){
            $_secs[$sc['sector_id']] = $sc;
        }
        if (1 == $this->m_user['user_id']) {
            $arr = list_to_tree($secs,'sector_id','parent_sector','_child', 0 );
        }
        else {
            $arr = array();
            $arr[]=$_secs[$this->m_user['sector_id']];
		    $arr[0]['_child'] = list_to_tree($secs,'sector_id','parent_sector','_child', $this->m_user['sector_id']);
        }
	   $res['sector'] = $arr;
		e_json( $res );
	}
	 /*添加离职*/
	public function addDimission() {
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        $uid = I('post.add_teacher',false,'intval');
        $MUser=D("Dimission");
        $wheres['user_id']=I('post.add_teacher',false,'intval');
        $ulist = M('User')->where( $wheres )->find();
        $wlist = M('UserExtension')->where( $wheres )->find();
        $ui=$this->m_user;
		$edit_data = array();
		$edit_data['dim_whoid']  = $ui['user_id'];
		$edit_data['user_id'] = I('post.add_teacher',false,'intval');
		$edit_data['dim_sector'] =  $ulist['sector_id'];
		$edit_data['dim_role'] =  $ulist['role_id'];
		$edit_data['dim_exstatus'] = $wlist['ext_user_status'];
		$edit_data['dim_date'] = I('post.time','','trim');
		$edit_data['dim_type'] = I('post.status','','trim');
		$edit_data['dim_reason'] = I('post.reson','','trim');
		$edit_data['dim_beizhu'] = I('post.beizhu','','trim');
		$edit_data['dim_updatetime'] =date("Y-m-d H-i-s") ;
	    $edit_data['dim_status'] = 1;
		$addres = $MUser->updateUserInfo('',$uid,$edit_data);
        $res['rcode'] = $addres['r'];
		$res['data'] = $addres['d'];
		$res['msg'] = $addres['i'];
		e_json($res);
	 }
	/* 自动补全展示*/
    public function addDimissionShow(){
    	$nickname = I('get.nickname');
    	$wheres = array();
    	$wheres['user_extension.ext_name'] = array('like','%'.$nickname.'%');
    	$wheres['user.state'] =array('neq',-1);
    	$userList = M("User")
    			->field("user.*,sector.sector_name,role.role_name,user_extension.ext_name,user_extension.ext_yggh")
    			->join("sector on sector.sector_id=User.sector_id")
    			->join("role on role.role_id=User.role_id")
    			->join("user_extension on user_extension.user_id=User.user_id")
    			->where($wheres)
    			->select();
    	for ($i=0; $i < count($userList); $i++) { 
    		if($userList[$i]['ext_name'] && $userList[$i]['sector_name'])
    	$userList[$i]['nickname_long'] =$userList[$i]['ext_name'].'  ('.$userList[$i]['ext_yggh'].')  '. $userList[$i]['sector_name'].',  '.$userList[$i]['role_name'];
    	}
    	if($userList){
    		$res['rcode'] = 1;
    		$res['data'] = $userList;
    	}else{
    		$res['msg'] = 'm';
    	}
    	e_json($res);
     }
     /*修改离职*/
    public function saveEdit(){
		$res = array('rcode'=>0,'msg'=>'服务器忙，请稍候再试');
		$ui=$this->m_user;
		$uid = I('post.dim_id');
		$edit_data['dim_date'] = I('post.time2');
		$edit_data['dim_type'] =  I('post.edit_leaveType');
		$edit_data['dim_reason'] =I('post.reson');
		$edit_data['dim_beizhu'] = I('post.beizhu');
		$edit_data['dim_updatetime'] =date("Y-m-d H-i-s") ;
		$edit_data['dim_whoid']  = $ui['user_id'];
		if(!$edit_data){
			$res['msg'] = "获取表单数据失败！";
		}else{
		   $addres = D("Dimission")->updateUserInfo(2,$uid,$edit_data);
			$res['rcode'] = $addres['r'];
			$res['data'] = $addres['d'];
			$res['msg'] = $addres['i'];			
		}
		e_json( $res );
 	 }
 	 /*撤销离职*/
 	public function deleteDimission(){
 		$res = array('rcode'=>0,'msg'=>'服务器忙，请稍候再试');
 		$uid = $_POST['dim_id'];
 		$ui=$this->m_user;
		$edit_data = array();
		$edit_data['user_id'] = I('post.user_id');
		$edit_data['dim_whoid']  = $ui['user_id'];
		$edit_data['dim_updatetime'] =date("Y-m-d H-i-s") ;
		$edit_data['dim_status'] = 2;
 		$addres = D("Dimission")->updateUserInfo(1,$uid, $edit_data);
			$res['rcode'] = $addres['r'];
			$res['data'] = $addres['d'];
			$res['msg'] = $addres['i'];	
 		    e_json( $res);
 	}
};